package com.alex.megacj.filter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alex.megacj.handler.RequestContext;

public class EroCheatFilter extends AbstractMatchingContentFilter {

	private static final Pattern HEAD_PATTERN = Pattern.compile("<head[^>]*>", Pattern.CASE_INSENSITIVE);
	
	@Override
	public String filterContent(String page, RequestContext context) {
		if (context.aliasHost == null) {
			return page;
		}
		
		String text = "<script type=\"text/javascript\" src=\"/script.php\"></script>";
		Matcher matcher = HEAD_PATTERN.matcher(page);
		if (matcher.find()) {
			page = page.substring(0, matcher.end()) + text + page.substring(matcher.end());
		}
		return page;
	}

}
